package service

import (
	baseStore "gitee.com/zaiqiang231/go-base-app/base_app/store"
	"gitee.com/zaiqiang231/zcamp-service-camp/app/model"
	"gitee.com/zaiqiang231/zcamp-service-camp/app/model/req"
	"github.com/pkg/errors"
	"gorm.io/gorm"
)

type ServiceActivity struct{}

var ServiceActivityInstance = new(ServiceActivity)

func (service *ServiceActivity) ActivityCreate(request req.ActivityCreateReq) (err error) {
	baseStore.GetDataStore().Execute(func(db *gorm.DB) {
		err = db.Transaction(func(tx *gorm.DB) error {
			txErr := tx.Create(&request.Activity).Error
			if txErr != nil {
				return txErr
			}
			return nil
		})
	})
	return
}

func (service *ServiceActivity) ActivityUpdateForMap(info map[string]interface{}) (err error) {
	baseStore.GetDataStore().Execute(func(db *gorm.DB) {
		err = db.Transaction(func(tx *gorm.DB) error {
			var old model.Activity
			var txErr error
			if errors.Is(tx.Where("id = ?", info["id"]).First(&old).Error, gorm.ErrRecordNotFound) {
				txErr = errors.New("原记录不存在")
			}
			if txErr != nil {
				return txErr
			}

			delete(info, "updated_at")
			txErr = tx.Model(&old).Omit("id", "created_at", "deleted_at").Updates(info).Error
			if txErr != nil {
				return txErr
			}

			return nil
		})

	})
	return err
}

func (service *ServiceActivity) DeleteActivityList(info req.IdsReq) (err error) {
	baseStore.GetDataStore().Execute(func(db *gorm.DB) {
		var tmpList []model.Activity
		err = db.Model(&model.Activity{}).Where("id in ?", info.Ids).Find(&tmpList).Delete(&tmpList).Error
	})
	return
}

func (service *ServiceActivity) GetActivityList(info req.ActivityListReq) (list interface{}, total int64, err error) {
	baseStore.GetDataStore().Execute(func(db *gorm.DB) {
		limit := info.PageSize
		offset := info.PageSize * (info.Page - 1)
		var tempDb = db
		if info.ID != 0 {
			tempDb = tempDb.Where("id = ?", info.ID)
		}
		if info.Title != "" {
			tempDb = tempDb.Where("camp_name LIKE ?", "%"+info.Title+"%")
		}
		if info.Status != 0 {
			tempDb = tempDb.Where("status = ?", info.Status)
		}
		var tmpList []model.Activity
		err = tempDb.Model(&model.Activity{}).Count(&total).Error
		if err != nil {
			return
		}
		err = tempDb.Model(&model.Activity{}).Offset(offset).Limit(limit).Find(&tmpList).Error
		if err != nil {
			return
		}
		list = tmpList
	})
	return
}

func (service *ServiceActivity) ActivityList(info req.ActivityListReq) (list interface{}, total int64, err error) {
	baseStore.GetDataStore().Execute(func(db *gorm.DB) {
		limit := info.PageSize
		offset := info.PageSize * (info.Page - 1)
		var tempDb = db
		var tmpList []model.Activity
		err = tempDb.Model(&model.Activity{}).Count(&total).Error
		if err != nil {
			return
		}
		err = tempDb.Model(&model.Activity{}).Offset(offset).Limit(limit).Find(&tmpList).Error
		if err != nil {
			return
		}
		list = tmpList
	})
	return
}

func (service *ServiceActivity) ActivityListForCamp(info req.ActivityListForCampReq) (list interface{}, total int64, err error) {
	baseStore.GetDataStore().Execute(func(db *gorm.DB) {
		limit := info.PageSize
		offset := info.PageSize * (info.Page - 1)
		var tempDb = db.Where("camp_id = ?", info.CampID)
		var tmpList []model.Activity
		err = tempDb.Model(&model.Activity{}).Count(&total).Error
		if err != nil {
			return
		}
		err = tempDb.Model(&model.Activity{}).Offset(offset).Limit(limit).Find(&tmpList).Error
		if err != nil {
			return
		}
		list = tmpList
	})
	return
}

func (service *ServiceActivity) ActivityRecommendList() (list interface{}, err error) {
	baseStore.GetDataStore().Execute(func(db *gorm.DB) {
		var tempDb = db
		var tmpList []model.Activity
		err = tempDb.Model(&model.Activity{}).Limit(5).Find(&tmpList).Error
		if err != nil {
			return
		}
		list = tmpList
	})
	return
}

func (service *ServiceActivity) ActivityDetail(request req.ActivityDetailReq) (data model.Activity, err error) {
	baseStore.GetDataStore().Execute(func(db *gorm.DB) {
		var tmp model.Activity
		err = db.Model(model.Activity{}).Where("id = ?", request.ActivityId).First(&tmp).Error
		data = tmp
	})
	return
}
